$(document).ready(function(){
    var tabletools = new TableTools();
    WarnValue_new();
    function WarnValue_new()
    {

        var warnval;
        $(document).off('click', '#commit-warnvalue');
        $(document).off('change', '.value_area');

        $.ajax({
            type: "GET",
            //url:"/Quiz/quiz/getWarnValue/oid/8/quiz_grouptitle/test",
            url:module_name+"/Orgnizer/getWarnValue/quiz_grouptitle/" +$("#quiz_title_select").val() ,
            dataType: "json",
            async:false,
            success: function (data) {
                warnval = data;
            }, error: function (jqXHR) {
                alert("加载预警值列表failed：" + jqXHR.status);
            }
        });

        var option_list = warnval.quiz_list;
        console.log(option_list);

        var htmls = [];
        for (var i = 0; i < Object.keys(option_list).length; i++){
            htmls.push('<option>'+option_list[i]+'</option>');
        }
        $("#warnvalue_select").html(htmls.join(''));

        $("#warnvalue_select").change(function(){
            pushValue($(this).val());
        });

        pushValue($("#warnvalue_select").val());

        $(document).on('change', '.value_area',function(){
            i = 0;
            var quiz_name = $("#warnvalue_select").val();

            var warnvalue = warnval.warnvalue_list[quiz_name];
            $("#warn-set-list").find('tr').each(function(){

                warnvalue[i].warn_value = $(this).find('td').eq(2).find('input').val();
                warnvalue[i++].warn_select = $(this).find('td').eq(1).find('select').get(0).selectedIndex;
            });

            //console.log(warnvalue);

            warnval.warnvalue_list[quiz_name] = warnvalue;

            // console.log(warnval.warnvalue_list);
            console.log(warnval);

        } );

        $(document).on('click', '#commit-warnvalue', function(e){
            e.stopPropagation();
            alert("开始提交");

            var json = {};

            for (var k in warnval.quiz_list){
                var rownum = 0;
                var quiz_name  = warnval.quiz_list[k];
                var val = warnval.warnvalue_list[quiz_name];
                json[quiz_name] = {};
                for (var i in val){
                    var rowobj = {};
                    rowobj[val[i].element_label] =  val[i].warn_value;
                    rowobj.select =  val[i].warn_select;
                    json[quiz_name][rownum++] = rowobj
                }
            }
            console.log(json);
            json = JSON.stringify(json);
            console.log(json);

            $.ajax({
                type: "POST",
                //url: "/Quiz/quiz/setWarnValue/oid/8/quiztitle/test",
                url:module_name+"/Orgnizer/setWarnValue/quiz_grouptitle/" +$("#quiz_title_select").val() ,
                dataType: "json",
                async: false,
                data:{
                    jsondata: json
                },
                success: function (data) {
                    alert(data.msg);
                    pushValue($("#warnvalue_select").val());

                }, error: function (jqXHR) {
                    alert("failed" + jqXHR.status);
                }
            });
        });

        // $("#commit-warnvalue").click();

        function pushValue(quiz_name)
        {
            var htmls = [];
            var warnvalue = warnval.warnvalue_list[quiz_name];

            for (i = 0; i < Object.keys(warnvalue).length; i++){
                htmls.push("<tr><td>"+warnvalue[i].element_name+"</td>");
                htmls.push("<td><select class='value_area'> <option>小于</option> <option>大于</option> </select></td>");
                htmls.push('<td><input type="number" class="value_area" id="'+warnvalue[i].element_label+'"></td></tr>');
            }

            $("#warn-set-list").html(htmls.join(''));

            i = 0;
            $("#warn-set-list").find('tr').each(function(){
                $input = $(this).find('td').eq(2).find('input');
                $input.val(warnvalue[i].warn_value);
                $select = $(this).find('td').eq(1).find('select');
                $select.get(0).selectedIndex= warnvalue[i].warn_select;
                i++;
            });
        }

    }


});